#!/bin/bash
############
# Settings #
############
PROJECT=sample_project
VERSION=1.3
# Set to 1 to get the lastest code from SVN and append the SVN version number:
SVN_LATEST=1
#Need the explicit name here, like ../../PARENTNAME:
PARENT_DIR=../../../sample_project
#Final installation target directory:
INSTALL_DIR=/usr/lib/sample_project
#Excluded file list (regex):
EXCLUDE_LIST=EXCLUDE.txt
#Source file list:
SOURCE_LIST=source_files.txt
#Installed file list:
INSTALLED_LIST=${PARENT_DIR}/INSTALLED_FILES.txt
#RPM spec filename:
SPEC_FILE=$PROJECT.spec
# Typically, you want to build as a user, but if you need
# to build as root, change this to:
#   /usr/src/redhat
# Unfortunately, I haven't tested whether this works as root.
RPM_ROOT=/var/rpmbuild
#QA server location to push RPM to when complete:
PUSH_QA=dev.example.net:/tmp/.

# Turn on bash's error checking:
set -e
set -u

########################################
# Get latest files and version number. #
########################################
if [ $SVN_LATEST == 1 ];
then
  echo "GETTING SVN UPDATES."
  svn up $PARENT_DIR
  # Append SVN version number to version:
  SVNVERSION=svn`svnversion`
  VERSION=${VERSION}${SVNVERSION}
  echo "GOT SVN VERSION $SVNVERSION."
fi

##############################
# Rewrite the RPM spec file. #
##############################
echo %define version ${VERSION} > $SPEC_FILE
cat $SPEC_FILE.base >> $SPEC_FILE
echo "Grant group write-access on sample_project.spec...please enter password when prompted."
sudo chown :wheel $SPEC_FILE
sudo chmod g+w $SPEC_FILE

#########################
# Build source tarball. #
#########################
TARBALL=${PROJECT}-${VERSION}
echo BUILDING TARBALL FOR $TARBALL...
if [ -d "$TARBALL" ] ; then
  echo "Directory already symlinked."
else
  echo "Symlink directory, so that the tarball gets the right name for RPM."
  ln -s $PARENT_DIR ./$TARBALL
fi
#NOTE: You must specify "-type" or it will include directories.
echo Get a clean list of source files.
find $TARBALL/* -type f | grep -vf $EXCLUDE_LIST > $SOURCE_LIST
echo Get a clean list of installed files.
cat $SOURCE_LIST | sed 's|^'$TARBALL'|"'$INSTALL_DIR'|' | sed -u 's/$/"/' > $INSTALLED_LIST
echo "Tar up relevant files."
tar -c -f $TARBALL.tar -T $SOURCE_LIST --no-recursion
#echo Adding INSTALLED_LIST to tarball.
#tar -rf $TARBALL.tar $INSTALLED_LIST
echo "Adding SPEC file to tarball."
tar -rf $TARBALL.tar $SPEC_FILE
echo "Unlinking source directory."
unlink $TARBALL
echo "Zipping archive."
mv $TARBALL.tar $TARBALL
gzip -S .tgz --force $TARBALL
echo "Grant group write-access on tarball...please enter password when prompted."
sudo chown :wheel $TARBALL.tgz
sudo chmod g+w $TARBALL.tgz
echo "DONE BUILDING TARBALL."

#############
# Build RPM #
#############
echo "BUILDING RPM..."
echo "Copying spec file to SPECS..."
cp $SPEC_FILE $RPM_ROOT/SPECS/
echo "Copying tarball to SOURCES..."
mv $TARBALL.tgz $RPM_ROOT/SOURCES/
echo "Checking for RPM macros definition at ~/.rpmmacros..."
if [ -f ~/.rpmmacros ];
then
  echo "Found existing RPM macros at ~/.rpmmacros. Check there if you have trouble after this point."
else
  echo "Writing RPM macros definition at ~/.rpmmacros..."
  echo '%_topdir /var/rpmbuild' > ~/.rpmmacros
fi;
echo "Beginning RPM build process at $RPM_ROOT/SPECS/..."
export RPM_PYTHON_NOCOMPILE=True
cd $RPM_ROOT/SPECS/
rpmbuild -ba $SPEC_FILE
echo "DONE BUILDING RPM."

##############
# Push to QA #
##############
echo "Pushing to $PUSH_QA ..."
scp $RPM_ROOT/RPMS/noarch/$PROJECT-$VERSION-1.noarch.rpm $PUSH_QA

########
# DONE #
########
echo "RPM BUILD PROCESS COMPLETE."
